package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "ClassLoginFilter",urlPatterns = "/classMain")
public class ClassLoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) req;
        Cookie[] cookies = httpServletRequest.getCookies();
        String isLogin = null;
        if (cookies != null) {
            for (Cookie tc : cookies) {
                if (tc.getName().equals("isLogin")) {
                    isLogin = tc.getValue();
                }
            }
        }
        if (null == isLogin) {
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        } else {
            chain.doFilter(req, resp);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
